
package no.ffi.tpm;

import java.io.Serializable;
import java.security.PublicKey;
import no.ffi.tpm.crypto.SignedObject;


/**
 * This class is used to encapsulate the data produced by the TPM during a key 
 * certification process, so that no TPM specific structures are needed on 
 * the recipient side.
 * 
 * @author Federico Mancini
 */
public class LegacyKeyCertifyInfo extends SignedObject implements Serializable {


    // The modulus of the public key being certified
    private PublicKey certifiedKey;
    //The AIK that was used to sign the TPM_CERTIFY_INFO structure
    public  transient PublicKey certifyingKey;
    private String subjId;
    //The serial number associated to the AIK used
    private final String aikSNR;
    //The index of the certifiedKey digest in the plainData array
    private int keyDigestIdx;
    //The index of the nonce in the plainData array
    private int nonceIdx;

    /**
     * 
     * @param plainData The TPM_CERTIFY_INFO structure with the information about the certified key. This is hashed and signed with the AIK
     * @param certifiedKey
     * @param certifyingKey
     * @param signature The AIKsignature produced from the TPM_CERTIFY_INFO structure and the AIK
     * @param aikSNR
     * @param digestIdx
     * @param nonceId 
     */
    public LegacyKeyCertifyInfo(byte[] plainData, PublicKey certifiedKey,PublicKey certifyingKey, byte[] signature,String aikSNR, int digestIdx, int nonceId) {
        super(plainData,signature);
        
        this.certifiedKey = certifiedKey;
        this.certifyingKey = certifyingKey;
    
        
        this.aikSNR=aikSNR;
        this.keyDigestIdx=digestIdx;
        this.nonceIdx=nonceId;
    }

    /**
     * @return the certifiedKey
     */
    public PublicKey getCertifiedKey() {
        return certifiedKey;
    }
    
     /**
     * @return the certifiedKey hash
     */
    public byte[] getCertifiedKeySHA1() {
        byte[] lkHash=new byte[20];
        System.arraycopy(this.object, this.keyDigestIdx, lkHash, 0, 20);
        return lkHash;
    }

    
      /**
     * @return the nonce
     */
    public byte[] getNonce() {
        byte[] nonce=new byte[20];
        System.arraycopy(this.object, nonceIdx, nonce, 0, 20);
        return nonce;
    }

    /**
     * @return the subjId
     */
    public String getSubjId() {
        return subjId;
    }

    /**
     * @param subjId the subjId to set
     */
    public void setSubjId(String subjId) {
        this.subjId = subjId;
    }

    /**
     * @return the aikSNR
     */
    public String getAikSNR() {
        return aikSNR;
    }

    
    
}
